home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / htfwrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  7.6 KB  |  353 lines

  1. /*        FILE WRITER                HTFWrite.h
  2. **        ===========
  3. **
  4. **    This version of the stream object just writes to a C file.
  5. **    The file is assumed open and left open.
  6. **
  7. **    Bugs:
  8. **        strings written must be less than buffer size.
  9. */
  10. #include"capalloc.h"
  11. #include"capstdio.h"
  12. #include "HTFWriter.h"
  13.  
  14. #include "HTFormat.h"
  15. #include "HTAlert.h"
  16. #include "HTFile.h"
  17. #include<dir.h>
  18.  
  19. /*        Stream Object
  20. **        ------------
  21. */
  22.  
  23. struct _HTStream {
  24.     CONST HTStreamClass *    isa;
  25.     
  26.     FILE *            fp;
  27.     char *             end_command;
  28.     char *             remove_command;
  29.     BOOL            announce;
  30. };
  31.  
  32.  
  33. /*_________________________________________________________________________
  34. **
  35. **        B L A C K    H O L E    C L A S S
  36. **
  37. **    There is only one black hole instance shared by anyone
  38. **    who wanst a black hole.  These black holes don't radiate,
  39. **    they just absorb data.
  40. */
  41. PRIVATE void HTBlackHole_put_character ARGS2(HTStream *, me, char, c)
  42. {}
  43. PRIVATE void HTBlackHole_put_string ARGS2(HTStream *, me, CONST char*, s)
  44. {}
  45. PRIVATE void HTBlackHole_write ARGS3(HTStream *, me, CONST char*, s, int, l)
  46. {}
  47. PRIVATE void HTBlackHole_free ARGS1(HTStream *, me)
  48. {}
  49. PRIVATE void HTBlackHole_abort ARGS2(HTStream *, me, HTError, e)
  50. {}
  51.  
  52.  
  53. /*    Black Hole stream
  54. **    -----------------
  55. */
  56. PRIVATE CONST HTStreamClass HTBlackHoleClass =
  57. {        
  58.     "BlackHole",
  59.     HTBlackHole_free,
  60.     HTBlackHole_abort,
  61.     HTBlackHole_put_character,     HTBlackHole_put_string,
  62.     HTBlackHole_write
  63. }; 
  64.  
  65. PRIVATE HTStream HTBlackHoleInstance =
  66. {
  67.     &HTBlackHoleClass,
  68.     NULL,
  69.     NULL,
  70.     NULL,
  71.     NO
  72. };
  73.  
  74. /*    Black hole craetion
  75. */
  76. PUBLIC HTStream * HTBlackHole NOARGS
  77. {
  78.     return &HTBlackHoleInstance;
  79. }
  80.  
  81.  
  82. /*_________________________________________________________________________
  83. **
  84. **        F I L E     A C T I O N     R O U T I N E S
  85. **  Bug:
  86. **    All errors are ignored.
  87. */
  88.  
  89. /*    Character handling
  90. **    ------------------
  91. */
  92.  
  93. PRIVATE void HTFWriter_put_character ARGS2(HTStream *, me, char, c)
  94. {
  95.     putc(c, me->fp);
  96. }
  97.  
  98.  
  99.  
  100. /*    String handling
  101. **    ---------------
  102. **
  103. **    Strings must be smaller than this buffer size.
  104. */
  105. PRIVATE void HTFWriter_put_string ARGS2(HTStream *, me, CONST char*, s)
  106. {
  107.     fputs(s, me->fp);
  108. }
  109.  
  110.  
  111. /*    Buffer write.  Buffers can (and should!) be big.
  112. **    ------------
  113. */
  114. PRIVATE void HTFWriter_write ARGS3(HTStream *, me, CONST char*, s, int, l)
  115. {
  116.     fwrite(s, 1, l, me->fp); 
  117. }
  118.  
  119.  
  120.  
  121.  
  122. /*    Free an HTML object
  123. **    -------------------
  124. **
  125. **    Note that the SGML parsing context is freed, but the created
  126. **    object is not,
  127. **    as it takes on an existence of its own unless explicitly freed.
  128. */
  129. PRIVATE void HTFWriter_free ARGS1(HTStream *, me)
  130. {
  131.     fclose(me->fp);
  132.     if (me->end_command) {        /* Temp file */
  133.         HTProgress(me->end_command);    /* Tell user what's happening */
  134.     system(me->end_command);
  135.     free (me->end_command);
  136.     if (me->remove_command) {
  137.         system(me->remove_command);
  138.         free(me->remove_command);
  139.     }
  140.     }
  141.  
  142.     free(me);
  143. }
  144.  
  145. /*    End writing
  146. */
  147.  
  148. PRIVATE void HTFWriter_abort ARGS2(HTStream *, me, HTError, e)
  149. {
  150.     fclose(me->fp);
  151.     if (me->end_command) {        /* Temp file */
  152.     if (TRACE) fprintf(stderr,
  153.         "HTFWriter: Aborting: file not executed.\n");
  154.     free (me->end_command);
  155.     if (me->remove_command) {
  156.         system(me->remove_command);
  157.         free(me->remove_command);
  158.     }
  159.     }
  160.  
  161.     free(me);
  162. }
  163.  
  164.  
  165.  
  166. /*    Structured Object Class
  167. **    -----------------------
  168. */
  169. PRIVATE CONST HTStreamClass HTFWriter = /* As opposed to print etc */
  170. {        
  171.     "FileWriter",
  172.     HTFWriter_free,
  173.     HTFWriter_abort,
  174.     HTFWriter_put_character,     HTFWriter_put_string,
  175.     HTFWriter_write
  176. }; 
  177.  
  178.  
  179. /*    Subclass-specific Methods
  180. **    -------------------------
  181. */
  182.  
  183. PUBLIC HTStream* HTFWriter_new ARGS1(FILE *, fp)
  184. {
  185.     HTStream* me;
  186.     
  187.     if (!fp) return NULL;
  188.  
  189.     me = (HTStream*)malloc(sizeof(*me));
  190.     if (me == NULL) outofmem(__FILE__, "HTML_new");
  191.     me->isa = &HTFWriter;       
  192.  
  193.     me->fp = fp;
  194.     me->end_command = NULL;
  195.     me->remove_command = NULL;
  196.     me->announce = NO;
  197.  
  198.     return me;
  199. }
  200.  
  201. /*    Make system command from template
  202. **    ---------------------------------
  203. **
  204. **    See mailcap spec for description of template.
  205. */
  206. /* @@ to be written.  sprintfs will do for now.  */
  207.  
  208.  
  209.  
  210. /*    Take action using a system command
  211. **    ----------------------------------
  212. **
  213. **    originally from Ghostview handling by Marc Andreseen.
  214. **    Creates temporary file, writes to it, executes system command
  215. **    on end-document.  The suffix of the temp file can be given
  216. **    in case the application is fussy, or so that a generic opener can
  217. **    be used.
  218. */
  219. PUBLIC HTStream* HTSaveAndExecute ARGS3(
  220.     HTPresentation *,    pres,
  221.     HTParentAnchor *,    anchor,    /* Not used */
  222.     HTStream *,        sink)    /* Not used */
  223.  
  224. #ifdef unix
  225. #define REMOVE_COMMAND "/bin/rm -f %s\n"
  226. #endif
  227. #ifdef VMS
  228. #define REMOVE_COMMAND "delete/noconfirm/nolog %s.."
  229. #endif
  230.  
  231. #ifdef REMOVE_COMMAND
  232. {
  233.     char *fnam;
  234.     CONST char * suffix;
  235.     
  236.     HTStream* me;
  237.     
  238.     if (HTClientHost) {
  239.         HTAlert("Can't save data to file -- please run WWW locally");
  240.     return HTBlackHole();
  241.     }
  242.  
  243.     me = (HTStream*)malloc(sizeof(*me));
  244.     if (me == NULL) outofmem(__FILE__, "Save and execute");
  245.     me->isa = &HTFWriter;  
  246.     
  247.     /* Save the file under a suitably suffixed name */
  248.     
  249.     suffix = HTFileSuffix(pres->rep);
  250.  
  251.     fnam = (char *)malloc (L_tmpnam + 16 + strlen(suffix));
  252.     tmpnam (fnam);
  253.     if (suffix) strcat(fnam, suffix);
  254.     
  255.     me->fp = fopen (fnam, "w");
  256.     if (!me->fp) {
  257.     HTAlert("Can't open temporary file!");
  258.         free(fnam);
  259.     free(me);
  260.     return NULL;
  261.     }
  262.  
  263. /*    Make command to process file
  264. */
  265.     me->end_command = (char *)malloc (
  266.                 (strlen (pres->command) + 10+ 3*strlen(fnam))
  267.                  * sizeof (char));
  268.     if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
  269.     
  270.     sprintf (me->end_command, pres->command, fnam, fnam, fnam);
  271.  
  272.     me->remove_command = NULL;    /* If needed, put into end_command */
  273. #ifdef NOPE
  274. /*    Make command to delete file
  275. */ 
  276.     me->remove_command = (char *)malloc (
  277.                 (strlen (REMOVE_COMMAND) + 10+ strlen(fnam))
  278.                  * sizeof (char));
  279.     if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
  280.     
  281.     sprintf (me->remove_command, REMOVE_COMMAND, fnam);
  282. #endif
  283.  
  284.     me->announce = NO;
  285.     free (fnam);
  286.     return me;
  287. }
  288.  
  289. #else    /* can do remove */
  290. { return NULL; }
  291. #endif
  292.  
  293.  
  294. /*    Save Locally
  295. **    ------------
  296. **
  297. **  Bugs:
  298. **    GUI Apps should open local Save panel here really.
  299. **
  300. */
  301. PUBLIC HTStream* HTSaveLocally ARGS3(
  302.     HTPresentation *,    pres,
  303.     HTParentAnchor *,    anchor,    /* Not used */
  304.     HTStream *,        sink)    /* Not used */
  305.  
  306. {
  307.     char *fnam;
  308.     char *answer;
  309.     CONST char * suffix;
  310.  
  311.     HTStream* me;
  312.     
  313.     if (HTClientHost) {
  314.         HTAlert("Can't save data to file -- please run WWW locally");
  315.     return HTBlackHole();
  316.     }
  317.     
  318.     me = (HTStream*)malloc(sizeof(*me));
  319.     if (me == NULL) outofmem(__FILE__, "SaveLocally");
  320.     me->isa = &HTFWriter;  
  321.     me->end_command = NULL;
  322.     me->remove_command = NULL;    /* If needed, put into end_command */
  323.     me->announce = YES;
  324.     
  325.     /* Save the file under a suitably suffixed name */
  326.     
  327.     suffix = HTFileSuffix(pres->rep);
  328.  
  329.     fnam = (char *)malloc (L_tmpnam + 16 + strlen(suffix));
  330.     tmpnam (fnam);
  331.     if (suffix) strcat(fnam, suffix);
  332.     
  333.     /*    Save Panel */
  334.     answer = HTPrompt("Give name of file to save in", fnam);
  335.     
  336.     free(fnam);
  337.     
  338.     me->fp = fopen (answer, "w");
  339.     if (!me->fp) {
  340.     HTAlert("Can't open local file to write into.");
  341.         free(answer);
  342.     free(me);
  343.     return NULL;
  344.     }
  345.  
  346.     free(answer);
  347.     return me;
  348. }
  349.  
  350. /*    Format Converter using system command
  351. **    -------------------------------------
  352. */
  353.